
package com.scs.application.modules.upms.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Maps;
import com.scs.application.core.exceptions.BusinessException;
import com.scs.application.core.service.BaseServiceImpl;
import com.scs.application.core.utils.StringUtils;
import com.scs.application.modules.upms.entity.WxmpAccount;
import com.scs.application.modules.upms.mapper.WxmpAccountMapper;
import com.scs.application.modules.upms.request.WxmpAccountQueryRequest;
import com.scs.application.modules.upms.service.WxmpAccountService;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.config.WxMpConfigStorage;
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
* 服务号 service 实现
*/
@Slf4j
@Service
public class WxmpAccountServiceImpl extends BaseServiceImpl<WxmpAccountMapper, WxmpAccount> implements WxmpAccountService, InitializingBean {

    private ConcurrentHashMap<String, WxMpService> accountCache = new ConcurrentHashMap<>();

    @Override
    public List<WxmpAccount> list(WxmpAccountQueryRequest request) {
        List<WxmpAccount> list = this.list(Wrappers.<WxmpAccount>query()
                .like(StringUtils.isNotBlank(request.getName()), "name", request.getName())
                .eq(StringUtils.isNotBlank(request.getAppId()), "app_id", request.getAppId())
            .orderByDesc("app_id")
        );
        return list;
    }

    @Override
    public IPage<WxmpAccount> page(WxmpAccountQueryRequest request) {
        IPage page = this.page(request.getPage(), Wrappers.<WxmpAccount>query()
                .like(StringUtils.isNotBlank(request.getName()), "name", request.getName())
                .eq(StringUtils.isNotBlank(request.getAppId()), "app_id", request.getAppId())
            .orderByDesc("app_id")
        );
        return page;
    }



    @Override
    public WxMpService getWxMpServiceFromAppId(String appId){

        WxMpService mpService = accountCache.get(appId);
        if(mpService != null){
            return mpService;
        }

        WxmpAccount account = this.getById(appId);
        if(account == null) {
            throw new BusinessException("不支持的服务号，请确认是否已经在系统维护");
        }
        WxMpDefaultConfigImpl config = new WxMpDefaultConfigImpl();
        config.setAppId(account.getAppId());
        config.setSecret(account.getAppSecret());
        config.setToken(account.getToken());
        config.setOauth2redirectUri(account.getOauthUrl());

        WxMpServiceImpl wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(config);
        accountCache.put(appId,wxMpService);
        return wxMpService;
    }

    @Override
    public Map<String, WxMpConfigStorage> getAllWxMpConfig(){
        List<WxmpAccount> accounts = this.list();
        Map<String, WxMpConfigStorage> configs = Maps.newHashMap();
//        for (WxmpAccount config : accounts) {
//            WxMpDefaultConfigImpl configStorage = new WxMpDefaultConfigImpl();
//            configStorage.setAppId(config.getAppId());
//            configStorage.setSecret(config.getAppSecret());
//            configStorage.setToken(config.getToken());
//            configStorage.setAesKey(config.getAesKey());
//            configStorage.setOauth2redirectUri(config.getOauthUrl());
//            configs.put(config.getMpKey(), configStorage);
//        }

        WxMpDefaultConfigImpl configStorage = new WxMpDefaultConfigImpl();
        configStorage.setAppId("x");
        configStorage.setSecret("x");
        configStorage.setToken("x");
        configStorage.setAesKey("x");
        configStorage.setOauth2redirectUri(null);
        configs.put("x", configStorage);
        return configs;
    }

    private void refreshCache(){

    }

    @Override
    public void afterPropertiesSet() throws Exception {
        refreshCache();
    }
}
